<?php
// Change the line below into true for debugging of this part of the script
$debugPaypal = false;

// Make connection and send the query
$gateways = new db;
$gateways->db_Select("e107shop_gateways", "*", "identifier='paypal'");

// Read out the Payson Values from the Gateway table
while ($gatewayRow=$gateways->db_Fetch()) {
    $PP_UserName    = $gatewayRow['account'];
    $PP_Paypal_Url  = $gatewayRow['field1'];
}

// Obtain the used language to know what button to display
switch (e_LANGUAGE) {
    case "Dutch":
        $languagecode = "NL";
        $languagebutton = "https://www.paypal.com/nl_NL/NL/i/btn/btn_paynow_LG.gif";
        break;
    case "French":
        $languagecode = "FR";
        $languagebutton = "https://www.paypal.com/fr_FR/FR/i/btn/btn_paynow_LG.gif";
        break;
    case "Italian":
        $languagecode = "IT";
        $languagebutton = "https://www.paypal.com/it_IT/IT/i/btn/btn_paynow_LG.gif";
        break;
    case "Persian":
        $languagecode = "US";
        $languagebutton = "http://www.paypal.com/en_US/i/btn/btn_paynow_LG.gif";
        break;
    default:
        if ($shop_settings["currency"] == "GBP") {
            $languagecode = "UK";
            $languagebutton = "http://www.paypal.com/en_US/i/btn/btn_paynow_LG.gif";
        } else {
            $languagecode = "US";
            $languagebutton = "http://www.paypal.com/en_US/i/btn/btn_paynow_LG.gif";
        }
}

// Create a new order id
$orderid = e107shop_order_id();

// We need this string replacements in the case the amount is in european format
if(substr($total, -3, 1) == ',' || substr($total, -2, 1) == ',') {
    $totalForPaypal = str_replace('.', 'DOT', $total);
    $totalForPaypal = str_replace(',', '.', $totalForPaypal);
    $totalForPaypal = str_replace('DOT', ',', $totalForPaypal);
} else {
    $totalForPaypal = $total;
}

// Build the form with the required parameters for Payson
$temp = "
<form action='".$PP_Paypal_Url."' method='post'>
<input type='hidden' name='cmd'           value='_xclick'>
<input type='hidden' name='business'      value='".$PP_UserName."'>
<input type='hidden' name='invoice'       value='".$orderid."'>
<input type='hidden' name='item_name'     value='".lan_outbound_admin_p02." ".$orderid."'>
<input type='hidden' name='amount'        value='".$totalForPaypal."'>
<input type='hidden' name='currency_code' value='".$shop_settings["currency"]."'>
<input type='hidden' name='charset'       value='utf-8'>
<input type='hidden' name='notify_url'    value='".$shop_settings['path']."paypal_notify.php'>
<input type='hidden' name='return'        value='".$shop_settings['path']."payment_accepted.php'>
<input type='hidden' name='cancel_return' value='".$shop_settings['path']."payment_canceled.php'>
<input type='hidden' name='no_note'       value='1'>
<input type='hidden' name='no_shipping'   value='1'>
<input type='hidden' name='lc'            value='".$languagecode."'>
<input type='hidden' name='bn'            value='PP-BuyNowBF'>
";

// build the payment button in the local language
$temp .= "<input type='image' src='{$languagebutton}' border='0' name='submit' alt='".lan_gateway_paypal_description."'></form>";

// Because we are gonna be redirected fromt he website we process the order before being redirected
e107shop_process_order($orderid, "pending", $cart_id, "paypal", $region, $coupon);

// If debug is activated spill out the variables used in this script
if (ADMIN && $debugPaypal) {
    $temp .= "
    <fieldset>
        <legend>Debug Information</legend>
        <b>Paypal Username:</b> ".$PP_UserName."<br />
        <b>Paypal Url:</b> ".$PP_Paypal_Url."<br />
        <b>Website Taal:</b> ".e_LANGUAGE."<br />
        <b>Orderid:</b> ".$orderid."<br />
        <b>Amount (Paypal):</b> ".$totalForPaypal."<br />
        <b>Currency Code:</b> ".$shop_settings['currency']."<br />
        <b>Paypal Notify:</b> ".$shop_settings['path']."paypal_notify.php<br />
        <b>CancelUrl:</b> ".$shop_settings['path']."paypal_canceled.php<br />
        <b>Shop Path:</b> ".$shop_settings['path']."<br />
        <b>Language Code:</b> ".$languagecode."<br />
        <b>Locale Setting:</b> ".setlocale(LC_ALL, 0)."<br />
        <b>Locale Environment:</b> ".var_dump(localeconv())."
    </fieldset>
    ";
}

// All is done, return the form to the originating calling script
return $temp;
